package com.linkwedo.saas.member;

import java.io.UnsupportedEncodingException;

import javax.naming.NamingException;

import org.apache.commons.mail.EmailException;

import com.linkwedo.saas.directory.LDAPService;
import com.linkwedo.saas.email.EmailService;
import com.linkwedo.saas.support.BaseAction;

public class ResetPasswordAction extends BaseAction{

	private static final long serialVersionUID = 6243051920379049597L;

	public String sendEmail() throws NamingException, UnsupportedEncodingException, EmailException {
		final String ERROR = "resetPassword_1_error";
		final String SUCCESS = "resetPassword_1_success";
		String email = getEmail();
		if (!LDAPService.isEmailExist(email)) {
			addActionError("Email doesn't exsit");
			return ERROR;
		}else {
			String verifyCode = getResetPasswordService().persistVerify(email);
			EmailService.sendResetPassword(email, verifyCode);
			return SUCCESS;
		}
	}
	
	public String resetPassword() throws NamingException {
		String password = getPassword();
		String email = getEmail();
		String userDn = LDAPService.getUserDN(email);
		if (userDn == null) {
			addActionError("Email doesn't exsit");
			return "resetPassword_error";
		}
		String rdn = LDAPService.getRDN(userDn);
		LDAPService.resetPassword(rdn, password);
		return "resetPassword_success";
	}
	
	public String toResetPassword_3() {
		return "toResetPassword_3";
	}
	
	public String toResetPassword() {
		return "toResetPassword_1";
	}
	
	private String email;
	private ResetPasswordService resetPasswordService;
	private String password;
	
	public ResetPasswordService getResetPasswordService() {
		return resetPasswordService;
	}

	public void setResetPasswordService(ResetPasswordService resetPasswordService) {
		this.resetPasswordService = resetPasswordService;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
